package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.dao.IGoodsService;
import cn.stylefeng.guns.dao.entity.*;
import cn.stylefeng.guns.dao.service.IStoreService;
import cn.stylefeng.guns.dao.service.ITestSetGoodsService;
import cn.stylefeng.guns.dao.service.ITestSetService;
import cn.stylefeng.guns.dao.service.ITestSetStoreService;
import cn.stylefeng.guns.modular.demo.controller.domain.PageRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.TestSetDetailVO;
import cn.stylefeng.guns.modular.demo.controller.domain.TestSetSaveRequest;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Test;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@ApiResource(name = "测试数据设置", resBizType = ResBizTypeEnum.BUSINESS, path = "/test-set")
public class TestSetController {

    @Resource
    private ITestSetService testSetService;
    @Resource
    private ITestSetGoodsService testSetGoodsService;
    @Resource
    private ITestSetStoreService testSetStoreService;
    @Resource
    private IGoodsService goodsService;
    @Resource
    private IStoreService storeService;

    @PostResource(name = "测试数据", path = "/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("测试数据")
    public ResponseData<PageResult<TestSet>> list(@RequestBody PageRequest pageRequest) {
        Page<TestSet> page = new Page<>();
        page.setCurrent(pageRequest.getPageNo());
        page.setSize(pageRequest.getPageSize());
        page = testSetService.page(page);
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }


    @PostResource(name = "测试数据详情", path = "/detail", requiredLogin = true, requiredPermission = true)
    @ApiOperation("测试数据详情")
    public ResponseData<TestSetDetailVO> detail(@RequestParam("testSetId") String testSetId) {

        TestSet testSet = testSetService.getById(testSetId);
        TestSetDetailVO testSetDetailVO = do2vo(testSet);

//        List<TestSetGoods> testSetGoods = testSetGoodsService.list(Wrappers.<TestSetGoods>lambdaQuery().eq(TestSetGoods::getTestSetId, testSetId));
        List<TestSetStore> testSetStores = testSetStoreService.list(Wrappers.<TestSetStore>lambdaQuery().eq(TestSetStore::getTestSetId, testSetId));
//        if (CollectionUtils.isNotEmpty(testSetGoods)) {
//            List<String> goodsIdList = testSetGoods.stream().map(TestSetGoods::getGoodsId).collect(Collectors.toList());
//            List<Goods> goods = goodsService.listByIds(goodsIdList);
//            testSetDetailVO.setGoodList(goods);
//        }
        if (CollectionUtils.isNotEmpty(testSetStores)) {
            List<String> storeIdList = testSetStores.stream().map(TestSetStore::getStoreId).collect(Collectors.toList());
            List<Store> stores = storeService.listByIds(storeIdList);
            testSetDetailVO.setStoreList(stores);
        }
        return new SuccessResponseData<>(testSetDetailVO);
    }

    @PostResource(name = "测试数据保存", path = "/save", requiredLogin = true, requiredPermission = true)
    @ApiOperation("测试数据保存")
    public ResponseData<Boolean> save(@RequestBody TestSetSaveRequest testSetSaveRequest) {
        String testSetId = testSetSaveRequest.getTestSetId();

        TestSet testSet;
        if (StringUtils.isEmpty(testSetId)) {
            testSet = new TestSet();
        } else {
            testSet = testSetService.getById(testSetId);
//            testSetGoodsService.remove(Wrappers.<TestSetGoods>lambdaQuery().eq(TestSetGoods::getTestSetId, testSetId));
            testSetStoreService.remove(Wrappers.<TestSetStore>lambdaQuery().eq(TestSetStore::getTestSetId, testSetId));
        }
        testSet = request2do(testSet, testSetSaveRequest);
        testSetService.saveOrUpdate(testSet);
        String testSetId1 = testSet.getTestSetId();
        List<String> goodList = testSetSaveRequest.getGoodList();
//        List<TestSetGoods> testSetGoodList = goodList.stream().map(r -> {
//            TestSetGoods testSetGoods = new TestSetGoods();
//            testSetGoods.setGoodsId(r);
//            testSetGoods.setTestSetId(testSetId1);
//            return testSetGoods;
//        }).collect(Collectors.toList());
//        if (CollectionUtils.isNotEmpty(testSetGoodList)) {
//            testSetGoodsService.saveBatch(testSetGoodList);
//        }

        List<String> storeList = testSetSaveRequest.getStoreList();
        List<TestSetStore> collect = storeList.stream().map(r -> {
            TestSetStore testSetGoods = new TestSetStore();
            testSetGoods.setStoreId(r);
            testSetGoods.setTestSetId(testSetId1);
            return testSetGoods;
        }).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {
            testSetStoreService.saveBatch(collect);
        }
        return new SuccessResponseData<>(true);
    }


    @ResponseBody
    @PostResource(name = "测试设置删除", path = "/delete", requiredLogin = true, requiredPermission = true)
    @ApiOperation("测试设置删除")
    @BusinessLog
    public ResponseData<Boolean> delete(String id) throws IOException {
        testSetService.removeById(id);
        testSetGoodsService.remove(Wrappers.<TestSetGoods>lambdaQuery().eq(TestSetGoods::getTestSetId, id));
        testSetStoreService.remove(Wrappers.<TestSetStore>lambdaQuery().eq(TestSetStore::getTestSetId, id));
        return new SuccessResponseData<>(true);
    }


    private TestSet request2do(TestSet testSet, TestSetSaveRequest testSetSaveRequest) {
        testSet.setTestSetId(testSetSaveRequest.getTestSetId());
        testSet.setName(testSetSaveRequest.getName());
        testSet.setDeliverNum(testSetSaveRequest.getDeliverNum());
        testSet.setSellNum(testSetSaveRequest.getSellNum());
        testSet.setGoodsNum(testSetSaveRequest.getGoodsNum());
        return testSet;
    }


    private TestSetDetailVO do2vo(TestSet testSet) {
        if (testSet == null) {
            return null;
        }
        TestSetDetailVO testSetDetailVO = new TestSetDetailVO();
        testSetDetailVO.setTestSetId(testSet.getTestSetId());
        testSetDetailVO.setName(testSet.getName());
        testSetDetailVO.setDeliverNum(testSet.getDeliverNum());
        testSetDetailVO.setSellNum(testSet.getSellNum());
        testSetDetailVO.setGoodsNum(testSet.getGoodsNum());
        testSetDetailVO.setCreateTime(testSet.getCreateTime().getTime());
        Date updateTime = testSet.getUpdateTime();
        if (Objects.nonNull(updateTime)) {
            testSetDetailVO.setUpdateTime(updateTime.getTime());
        }
        return testSetDetailVO;
    }


}
